// pages/applyAfterInfo/applyAfterInfo.js
const app = getApp()
import mode from '../../utils/mode'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    reasonForReturn: '',
    message: '',
    fileList: [],
    userInfo: null,
    order_id: '',
    user_id: '',
    order_goods: null,
    show: false,
    radio: '',
    actions: [],
    type: '',
    describe: '',
    order_goods_ids: [],
    imgs: '',
    orderList: '',
    orderInfo: null,
    id: '',
    pageFlag: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const info = wx.getStorageSync('uinfo')
    this.setData({
      userInfo: info,
      user_id: info.user.id,
      id: options.id || ''
    })

    let { type, order_id, orderInfo } = options
    if (order_id) {
      this.setData({
        order_id,
      })
      this.getOrderInfo(order_id)
    }
    if (orderInfo) {
      let data = JSON.parse(orderInfo)
      let num = 0
      data.order_goods.forEach(x => {
        x.selected = false
        num += x.number
      })
      if (data.order_goods.length == 1) data.order_goods[0].selected = true
      data.total = num
      if(data.order_status>1&&data.freight>0){
        let num = data.refund_money || data.order_real_pay_price
        data.real_back_price = (num - data.freight).toFixed(2)
      }
      this.setData({
        orderList: data.order_goods,
        orderInfo: data,
        order_id: data.order_id
      })
    }
    if (type) {
      this.setData({ type })
    }
    // 获取退款原因
    wx.$api.orderReason().then(res => {
      if (res.code) {
        this.setData({ actions: res.data })
      }
    })
  },
  inputChange (e) {
    this.setData({
      describe: e.detail
    })
  },
  onClose (e) {
    this.setData({
      show: false
    })
  },
  onClick (e) {
    this.setData({
      radio: e.currentTarget.dataset.name.id,
      reasonForReturn: e.currentTarget.dataset.name.title
    })
    setTimeout(() => {
      this.setData({ show: false })
    }, 300)
  },
  delImg (e) {
    let { fileList = [] } = this.data
    fileList.splice(e.detail.index, 1)
    this.setData({
      fileList: fileList
    })
  },
  afterRead (e) {
    const { file } = e.detail
    // return
    // 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
    wx.uploadFile({
      url: 'https://admin.2am-project.cn/api/twoam/user/upload', // 真实的接口地址
      filePath: file.url,
      name: 'files',
      success: (res) => {
        // 上传完成需要更新 fileList
        file.url = JSON.parse(res.data).data
        let { fileList } = this.data
        fileList.push(file)
        this.setData({ fileList })
      },
    })
  },
  handle () {
    this.setData({
      show: !this.data.show
    })
  },
  async getOrderInfo (order_id) {
    wx.showLoading()
    let { code, data } = await wx.$api.orderRefund({
      user_id: this.data.user_id,
      order_id
    })
    if (code == 1) {
      let num = 0
      data.order_goods.forEach(x => {
        x.selected = false
        num += x.number
      })
      if (data.order_goods.length == 1) data.order_goods[0].selected = true
      data.total = num
      if(data.order_status>1&&data.freight>0){
        let num = data.refund_money || data.order_real_pay_price
        data.real_back_price = (num - data.freight).toFixed(2)
      }
      this.setData({
        orderList: data.order_goods,
        orderInfo: data,
      })
    } else {
      wx.showToast({
        title: '获取订单数据失败！',
        icon: 'none',
        success: () => {
          setTimeout(() => {
            app.backPage()
          }, 1000)
        }
      })
    }
    setTimeout(() => {
      wx.hideLoading()
    }, 200)
  },
  selectList (e) {
    let { orderList, order_goods_ids } = this.data
    let { index } = e.currentTarget.dataset
    orderList[index].selected = !orderList[index].selected
    this.setData({ orderList, order_goods_ids })
  },
  //提交
  async submit () {
    let { type, order_id, radio, user_id, order_goods_ids, describe, imgs, orderList, fileList, id } = this.data
    if (type == 0 && !describe.trim()) {
      wx.showToast({
        title: '请填写退款原因！',
        icon: 'none'
      })
      return
    }
    let arr = []
    orderList.forEach(x => {
      if (x.selected) {
        arr.push(x.id)
      }
    })
    if (radio == '' && type != 0) {
      wx.showToast({
        title: type != 2 ? '请选择退货原因！' : '请选择换货的原因！',
        icon: 'none'
      })
      return
    }
    if (type == 2 && !arr.length) {
      wx.showToast({
        title: '请选择要换货的商品！',
        icon: 'none'
      })
      return
    }
    let imgArr = []
    fileList.forEach(x => {
      imgArr.push(x.url)
    })
    let { code, msg, data } = await wx.$api.orderRefundApply({
      order_reason_id: radio,
      order_id,
      type,
      user_id,
      describe,
      order_goods_ids: arr.join(','),
      imgs: imgArr.join(','),
      id
    })
    if (code) {
      wx.showToast({
        title: '提交成功！',
        success: () => {
          setTimeout(() => {
            wx.redirectTo({ url: '/pages/applyForService/applyForService?id=' + data })
          }, 500)
        }
      })
    } else {
      wx.showToast({
        title: msg,
        icon: 'none'
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

})